package krow.dev.gunpobus.control.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import krow.dev.gunpobus.Constants.Attribute;
import krow.dev.gunpobus.Constants.Value;
import krow.dev.gunpobus.GlobalStore;
import krow.dev.gunpobus.model.RouteItem;
import krow.dev.gunpobus.model.ScheduleItem;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

public class ScheduleParser implements IHtmlParser {

	private static Pattern sRouteIdPattern = Pattern.compile("(?:routeId=([0-9]+))");

	private List<String> mStationIdList;
	
	private List<ScheduleItem> mScheduleItemList;

	private ScheduleItem mCurrentItem;

	private Map<String, RouteItem> mRouteMap;
	
	public ScheduleParser() {

		mStationIdList = new ArrayList<String>();
		
		mScheduleItemList = new ArrayList<ScheduleItem>();
	
		mRouteMap = GlobalStore.getInstance().getRouteItemMap();
	}

	public List<ScheduleItem> getScheduleItemList() {

		return mScheduleItemList;
	}

	@Override
	public void parse(Source source) throws Exception {

		List<Element> elementList = source.getAllElements(HTMLElementName.TR);
		for (Element element : elementList) {
			parseRouteInformation(element);
			if (mCurrentItem != null) {
				parseEstimatedInformation(element);
			}
		}
	}

	private void parseRouteInformation(Element element) {

		String id = null;
		String name = null;
		String licensePlates = null;

		try {

			List<Element> spanElementList = element.getAllElements(HTMLElementName.SPAN);
			for (Element spanElement : spanElementList) {

				String className = spanElement.getAttributeValue(Attribute.CLASS);

				if (className.equals(Value.CLASS_ROUTE_NAME)) {
					name = spanElement.getTextExtractor().toString();
				} else if (className.equals(Value.CLASS_LICENSE_PLATES)) {
					licensePlates = spanElement.getTextExtractor().toString();
				}
			}

			List<Element> aElementList = element.getAllElements(HTMLElementName.A);
			for (Element aElement : aElementList) {
				Matcher matcher = sRouteIdPattern.matcher(aElement.getAttributeValue(Attribute.REFERENCE));
				if (matcher.find()) {
					id = matcher.group(1);
				}
			}
		} catch (Exception e) {

		}

		if (name != null && mRouteMap != null) {
			if(mStationIdList.indexOf(id) >= 0){
				return;
			}
			
			RouteItem routeItem = mRouteMap.get(id);
			if(routeItem != null){
				mCurrentItem = new ScheduleItem();
				mCurrentItem.setId(id);
				mCurrentItem.setType(routeItem.getType());
				mCurrentItem.setName(name);
				mCurrentItem.setLicensePlates(licensePlates);
				mScheduleItemList.add(mCurrentItem);
				mStationIdList.add(id);
			}
		}
	}

	private void parseEstimatedInformation(Element element) {

		List<Element> dataElementList = element.getAllElements(HTMLElementName.TD);
		for (Element dataElement : dataElementList) {

			if ("2".equals(dataElement.getAttributeValue(Attribute.COLUMN_SPAN))) {

				String text = dataElement.getTextExtractor().toString();
				if (text.indexOf("분") > 0) {
					mCurrentItem.setEstimatedMinute(text);
				} else if (text.indexOf("정류장") > 0 || text.indexOf("정류소") > 0) {
					mCurrentItem.setEstimatedStation(text);
				}
			}
		}
	}
}
